package com.meme.editor.gui.sprite2d;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;

import org.apache.log4j.Logger;

import com.meme.app.AppRegistry;
import com.meme.editor.EntityObject;
import com.meme.editor.gui.EntityChildrenListView;
import com.meme.editor.gui.PropertyEditPanel;
import com.meme.editor.model.sprite2d.CompositedModuleGroup;
import com.meme.editor.model.sprite2d.PureModuleGroup;
import com.meme.editor.model.sprite2d.ModuleGroup;
import com.meme.editor.model.sprite2d.AnimatedModuleGroup;
import com.meme.editor.model.sprite2d.Sprite2dFile;
import com.meme.editor.model.sprite2d.Sprite2dFile.CompositedModuleGroupSet;
import com.meme.editor.model.sprite2d.Sprite2dFile.PureModuleGroupSet;

public class AnimatedModuleContentPanel extends JPanel
{
	static Logger logger = Logger.getLogger (CompositedModuleContentPanel.class);
	
	JSplitPane mSplitPane_Children;
	JSplitPane mSplitPane_Editor;
	JSplitPane mSplitPane_GrandChildren;
	JSplitPane mSplitPane_View;
	JSplitPane mSplitPane_Main;
	JTabbedPane mTabbedPanel;
	
	public AnimatedModuleContentPanel (AnimatedModuleGroup animatedModuleGroup)
	{
		int width  = ( (Integer) AppRegistry.getRegistry("editor.sprite2d.contentPanel.width") ).intValue();
		int height = ( (Integer) AppRegistry.getRegistry("editor.sprite2d.contentPanel.height") ).intValue();

		mTabbedPanel = new JTabbedPane ();
		//mTabbedPanel.setTabPlacement(JTabbedPane.BOTTOM);
		PureModuleGroupSet pureModuleGroupSet = (PureModuleGroupSet) ( ( (Sprite2dFile) ( animatedModuleGroup.getHost() ) ).getPureModuleGroupSet() );	
		for (EntityObject group : pureModuleGroupSet.getChildren())
		{
			ModuleGroup moduleGroup = (ModuleGroup)group;			
			mTabbedPanel.add(moduleGroup.toString(), new ModuleGroupPanel ( moduleGroup, ModuleGroupPanel.ViewMode_Icons, ModuleGroupPanel.EditMode_CompositedModule, moduleGroup instanceof PureModuleGroup) );
		}
		CompositedModuleGroupSet compositedModuleGroupSet = (CompositedModuleGroupSet) ( ( (Sprite2dFile) ( animatedModuleGroup.getHost() ) ).getCompositedModuleGroupSet() );	
		for (EntityObject group : compositedModuleGroupSet.getChildren())
		{
			ModuleGroup moduleGroup = (ModuleGroup)group;			
			mTabbedPanel.add(moduleGroup.toString(), new ModuleGroupPanel ( moduleGroup, ModuleGroupPanel.ViewMode_Icons, ModuleGroupPanel.EditMode_CompositedModule, moduleGroup instanceof PureModuleGroup) );
		}
		
		
		EntityChildrenListView childrenView = new EntityChildrenListView (animatedModuleGroup);
		PropertyEditPanel modulePropertyPanel = new PropertyEditPanel ();
		childrenView.setPropertyEditPanel (modulePropertyPanel);

		EntityChildrenListView grandChildrenView = new EntityChildrenListView ();
		PropertyEditPanel moduleSequencePropertyPanel = new PropertyEditPanel ();
		grandChildrenView.setPropertyEditPanel (moduleSequencePropertyPanel);
		
		childrenView.setGrandChildrenView (grandChildrenView);
		
		AnimatedModuleView animationView = new AnimatedModuleView ();
		animationView.setMinimumSize(new Dimension (320, 320));
		animationView.setChildPropertyView(moduleSequencePropertyPanel);
		animationView.setChildrenListView(grandChildrenView);
		childrenView.getListComponent().addListSelectionListener (animationView);
		grandChildrenView.getListComponent().addListSelectionListener (animationView);
		
		AnimatedModulePanel  animatedMobulepanel = new AnimatedModulePanel (animationView);
		
		mSplitPane_GrandChildren = new JSplitPane (JSplitPane.VERTICAL_SPLIT, 
				moduleSequencePropertyPanel, 
				grandChildrenView 
                );
		mSplitPane_GrandChildren.setResizeWeight (0.0);
		mSplitPane_GrandChildren.setDividerSize  (2);
		mSplitPane_GrandChildren.setDividerLocation (height / 3);
		
		mSplitPane_View = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT, 
				animatedMobulepanel, 
				mSplitPane_GrandChildren 
                );
		mSplitPane_View.setResizeWeight (1.0);
		mSplitPane_View.setDividerSize  (2);
		mSplitPane_View.setDividerLocation (width * 2 / 5);
		
		mSplitPane_Editor = new JSplitPane (JSplitPane.VERTICAL_SPLIT, 
				mSplitPane_View, 
				mTabbedPanel 
                );
		mSplitPane_Editor.setResizeWeight (1.0);
		mSplitPane_Editor.setDividerSize  (2);
		mSplitPane_Editor.setDividerLocation (height * 2 / 3);
		
		mSplitPane_Children = new JSplitPane (JSplitPane.VERTICAL_SPLIT, 
				modulePropertyPanel, 
				childrenView 
                );
		mSplitPane_Children.setResizeWeight (0.0);
		mSplitPane_Children.setDividerSize  (2);
		mSplitPane_Children.setDividerLocation (60);

		mSplitPane_Main = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT, 
				mSplitPane_Editor, 
				mSplitPane_Children 
                );
		mSplitPane_Main.setResizeWeight (1.0);
		mSplitPane_Main.setDividerSize  (2);
		mSplitPane_Main.setDividerLocation (width * 2 / 3);
		
		setLayout ( new BorderLayout() );
		add (mSplitPane_Main, BorderLayout.CENTER);		
	}
}
